//
//  AppDelegate.swift
//  新浪微博
//
//  Created by 李国伟 on 15/12/6.
//  Copyright © 2015年 李国伟. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.rootViewController = defaultViewController
//        window?.rootViewController = PictureSelectController()
        window?.backgroundColor = UIColor.whiteColor()
        window?.makeKeyAndVisible()
        
        setUpApparence()
        changeRootController()
        return true
    }

    //设置导航栏按钮的颜色都为橙色
    private func setUpApparence(){
        UITabBar.appearance().tintColor = UIColor.orangeColor()
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
    }
    
    //定义一个默认
    private var defaultViewController: UIViewController{
        
        //如果没有登录就返回MainViewController
        if UserAccountViewModel.shareModel.userlogin{
            
            //判断是否是新版本
            return UserAccountViewModel.shareModel.isNewVersion ? NewFeatureController() : WelcomeViewController()
        }
        
        return MainViewController()
    }
    
    //进入后台后清空过期数据
    func applicationDidEnterBackground(application: UIApplication) {
        StatusDAL.clearMaxDate()
    }
    
    //注销通知中心
      deinit{
        
        NSNotificationCenter.defaultCenter() .removeObserver(self)
    }
}


//更换控制器
extension AppDelegate{
    
    func changeRootController () {
        
        NSNotificationCenter.defaultCenter().addObserverForName(WBSwitchRootViewControllerNotification, object: nil, queue: nil) { (notification) -> Void in
            
            if notification.object != nil{
                
                self.window?.rootViewController = WelcomeViewController()
            }else{
                
                self.window?.rootViewController = MainViewController()
            }
        }
    }
}
